﻿// See http://www.strathweb.com/2012/05/using-ninject-with-the-latest-asp-net-web-api-source/
using System.Web.Http.Dependencies;
using Ninject.Syntax;
using Ninject.Activation;
using System.Collections.Generic;
using System;
using System.Linq;
using Ninject.Parameters;
public class NinjectScope : IDependencyScope
{
    private IResolutionRoot resolutionRoot;

    public NinjectScope(IResolutionRoot kernel)
    {
        this.resolutionRoot = kernel;
    }

    public object GetService(Type serviceType)
    {
        IRequest request = this.resolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
        return resolutionRoot.Resolve(request).SingleOrDefault();
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        IRequest request = resolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
        return resolutionRoot.Resolve(request).ToList();
    }

    public void Dispose()
    {
        IDisposable disposable = this.resolutionRoot as IDisposable;

        if (disposable != null)
        {
            disposable.Dispose();
        }

        this.resolutionRoot = null;
    }
}